package com.guard.net;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.guard.android.Constants;
import com.guard.helper.StringHelper;
import com.guard.location.PatrolData;
import com.guard.structures.ClientStatus;
import com.guard.structures.SystemStatus;

public class ResponseParser {

    public static SystemStatus parseSystemStatus(HttpResponseContainer response) {
        if (response == null || response.isFailed()) {
            return null;
        }
        List<String> lines = StringHelper.getLinesList(response.getBody());
        SystemStatus status = new SystemStatus(true, ClientStatus.noAlarm, null);
        int size = lines.size();
        if (size > 1) {
            String codeStatus = lines.get(1);
            status.setStatus(getStatus(codeStatus));
            if (size >= 5) {
                PatrolData patrolData = status.getPatrolData();
                patrolData.setX(Double.valueOf(lines.get(2)));
                patrolData.setY(Double.valueOf(lines.get(3)));
                patrolData.setCarInfo(lines.get(4));
                patrolData.setPhoneNumber(lines.get(4));
            }
        }
        return status;
    }

    public static ClientStatus getStatus(String status) {
        int value = 0;
        ClientStatus clientStatus = ClientStatus.noAlarm;

        try {
            value = Integer.parseInt(status);
        } catch (NumberFormatException e) {
        }

        for (ClientStatus item : ClientStatus.values()) {
            if (item.getCode() == value)
                return item;
        }

        return clientStatus;
    }


    public static boolean isCanceled(HttpResponseContainer response) {

        List<String> params = StringHelper.getLinesList(response.getBody());

        return !params.isEmpty() && params.get(0).equals(Constants.CANCELED);
    }

    public static String parseRegisterData(HttpResponseContainer response) {
        if (response == null || response.isFailed()) {
            return null;
        }
        String body = response.getBody();
        body = body.replace("\n", "");
        if (body.equalsIgnoreCase("-1")) {
            return null;
        } else {
            return body;
        }
    }

    public static boolean parseSaveProfileResponse(HttpResponseContainer response) {
        if (response == null || response.isFailed()) {
            return false;
        }

        List<String> list = StringHelper.getLinesList(response.getBody());
        if (list.size() == 1) {
            if (list.get(0).equals("ne ok")) {
                return false;
            } else if (list.get(0).equals("ok")) {
                return true;
            }
        }
        return false;
    }

    public static double[] parseCoorData(HttpResponseContainer response) {
        if (response == null || response.isFailed()) {
            return null;
        }
        List<String> list = StringHelper.getLinesList(response.getBody());
        if (list.size() != 2) {
            return null;
        }
        double[] points = new double[2];
        try {
            int i = 0;
            for (String string : list) {
                double value = Double.parseDouble(string);
                points[i] = value;
                i++;
            }
        } catch (NumberFormatException e) {
            return null;
        }
        return points;
    }

    public static int parseSendSosData(HttpResponseContainer response) {
        if (response == null || response.isFailed()) {
            return -1;
        }

        List<String> list = StringHelper.getLinesList(response.getBody());
        if (list.size() == 1) {
            if (list.get(0).equals("No cookie")) {
                return 0;
            } else if (list.get(0).equals("Ok")) {
                return 1;
            }
        }
        return -1;
    }

}